package com.aube.plugin.gallery.preview;

import android.content.Context;
import android.graphics.Rect;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.android.volley.toolbox.Scheme;
import com.aube.plugin.gallery.R;
import com.aube.plugin.gallery.scaleview.IDecodeErrorListener;
import com.aube.plugin.gallery.scaleview.ImageSource;
import com.aube.plugin.gallery.scaleview.SubsamplingScaleImageView;
import com.huyn.baseframework.ImageLoader;
import com.huyn.baseframework.imgcompress.Luban;
import com.huyn.baseframework.imgcompress.OnCompressListener;
import com.huyn.baseframework.utils.StringUtils;
import com.huyn.baseframework.utils.Utils;

import java.io.File;

/**
 * Created by huyaonan on 16/8/30.
 */
public class ViewPagerLayout extends ViewPagerLayoutBase {

    private SubsamplingScaleImageView scaleImageView;

    public ViewPagerLayout(Context context) {
        this(context, null);
    }

    public ViewPagerLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ViewPagerLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void initViews() {
        inflate(R.layout.view_bigimage_scaleimg, this, true);
    }

    public void setAsset(final String asset) {
        RelativeLayout mLayout = (RelativeLayout) findViewById(R.id.img_root);

        final DonutProgress progressBar = new DonutProgress(context);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        mLayout.addView(progressBar, 2, params);

        final View error = findViewById(R.id.error);

        scaleImageView = new SubsamplingScaleImageView(context);
        mLayout.addView(scaleImageView, 0, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        final ImageView maskImg = (ImageView) findViewById(R.id.maskimg);
        if (asset != null) {
            if (iGetter != null && iGetter.getPreview() != null){
                maskImg.setVisibility(View.VISIBLE);
                maskImg.setImageBitmap(iGetter.getPreview());
                maskImg.setOnClickListener(mClickListener);
            }else {
                maskImg.setVisibility(View.GONE);
            }

            if(Scheme.ofUri(asset) == Scheme.FILE) {
                scaleImageView.setIDecodeErrorListener(new IDecodeErrorListener() {
                    @Override
                    public void onError() {
                        scaleImageView.post(new Runnable() {
                            @Override
                            public void run() {
                                error.setVisibility(View.VISIBLE);
                            }
                        });
                    }
                });
                Utils.sysout("open file:" + asset);
//                scaleImageView.setImage(ImageSource.uri(asset));
//                progressBar.setVisibility(View.GONE);
//                hideMask(maskImg);
                Luban.get(getContext()).load(new File(asset)).putGear(Luban.THIRD_GEAR).setCompressListener(new OnCompressListener() {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onSuccess(String dest) {
                        if(StringUtils.isBlank(dest)) {
                            return;
                        } else if("ERROR".equalsIgnoreCase(dest)) {
                            return;
                        }
                        scaleImageView.setImage(ImageSource.uri(asset));
                        progressBar.setVisibility(View.GONE);
                        hideMask(maskImg);
                    }

                    @Override
                    public void onError() {
                    }
                }).launch();
            } else {
                scaleImageView.setIDecodeErrorListener(new IDecodeErrorListener() {
                    @Override
                    public void onError() {
                        scaleImageView.post(new Runnable() {
                            @Override
                            public void run() {
                                //show error
                            }
                        });
                    }
                });
                ImageLoader.getInstance(context).preloadBitmapToFile(asset, 1000, 1000, new ImageLoader.IPreloadCallback<String>() {
                    @Override
                    public void onSuccess(String s) {
                        if(StringUtils.isBlank(s)) {
                            Utils.Log("load file fail:" + asset + " with size:1000x1000");
                            return;
                        }
                        Utils.Log("load file success:" + s + " with size:1000x1000");
//                        if(StringUtils.isNotBlank(s)) {
//                            progressBar.setVisibility(View.GONE);
//                            if(iGetter != null)
//                                iGetter.updateRes(s, mIndex);
//                            else
//                                Utils.sysout("iGetter is null...BAD!!!");
//                            scaleImageView.setImage(ImageSource.uri(s));
//                            hideMask(maskImg);
//                        }
                        Luban.get(getContext()).load(new File(s)).putGear(Luban.THIRD_GEAR).setCompressListener(new OnCompressListener() {
                            @Override
                            public void onStart() {

                            }

                            @Override
                            public void onSuccess(String dest) {
                                Utils.Log("compress file success:" + dest);
                                if(StringUtils.isNotBlank(dest)) {
                                    progressBar.setVisibility(View.GONE);
                                    if(iGetter != null)
                                        iGetter.updateRes(dest, mIndex);
                                    else
                                        Utils.sysout("iGetter is null...BAD!!!");
                                    scaleImageView.setImage(ImageSource.uri(dest));
                                    hideMask(maskImg);
                                }
                            }

                            @Override
                            public void onError() {
                            }
                        }).launch();
                    }

                    @Override
                    public void onFail() {
                        progressBar.setVisibility(View.GONE);
                    }
                });
                progressBar.setVisibility(View.VISIBLE);
            }
        }

        //必须要给imageview
        scaleImageView.setOnClickListener(mClickListener);
        scaleImageView.setOnLongClickListener(mLongClickListener);
    }

    private void hideMask(final View mask) {
        if (mask.getVisibility() == View.VISIBLE) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mask.setVisibility(View.GONE);
                }
            }, 200);
//            ObjectAnimator anim = ObjectAnimator.ofFloat(mask, "alpha", 1f, 0f);
//            anim.setDuration(300);
//            anim.addListener(new AnimatorListenerAdapter() {
//                @Override
//                public void onAnimationEnd(Animator animation) {
//                    mask.setVisibility(View.GONE);
//                    mask.setAlpha(1f);
//                }
//            });
//            anim.start();
        }
    }

    public Rect getCurrentImageRect(){
        if (scaleImageView != null){
            return scaleImageView.getCurrentImageRect();
        }
        return null;
    }
}
